package com.wxzz.elearing.pay.quartz;

import com.wxzz.elearing.pay.entity.PlatformLivetelecastOrderData;
import com.wxzz.elearing.pay.mapper.PlatformLivetelecastOrderDataMapper;
import com.wxzz.elearing.pay.vo.PlatformLivetelecastOrderDataVo;
import com.wxzz.elearning.common.constants.RedisConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class PlatformLivetelecastQuartz {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private PlatformLivetelecastOrderDataMapper platformLivetelecastOrderDataMapper;

    /**
     * 每天11：55执行
     */
    //@Scheduled(cron = "0 0 */4 * * ?")
    //@Scheduled(cron = "*/10 * * * * ?")
    //每隔5秒执行一次：*/5 * * * * ?
    @Scheduled(cron = "0 59 23 * * ?")
    public void liveStatisticsDataQuartz() {
        //分布式环境下进行幂等
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(RedisConstants.LIVE_STATISTICS_DATA_QUARTZ, RedisConstants.LIVE_STATISTICS_DATA_QUARTZ, 60 * 60, TimeUnit.SECONDS);
        if (!Boolean.TRUE.equals(aBoolean)) {
            //如果已存在则直接退出任务
            return;
        }
        String startTime = LocalDate.now().toString();
        String endTime = LocalDate.now().plusDays(1).toString();
        //查询直播收入
        List<PlatformLivetelecastOrderDataVo> platformLivetelecastOrderDataVos = platformLivetelecastOrderDataMapper.selectPlatformLivetelecastOrderDataVo(startTime, endTime, 1);
        for (PlatformLivetelecastOrderDataVo platformLivetelecastOrderDataVo : platformLivetelecastOrderDataVos) {
            if (platformLivetelecastOrderDataVo != null) {
                PlatformLivetelecastOrderData platformLivetelecastOrderData = new PlatformLivetelecastOrderData();
                platformLivetelecastOrderData.setAmount(platformLivetelecastOrderDataVo.getIncomePrice());
                platformLivetelecastOrderData.setType(0);
                platformLivetelecastOrderData.setSaveTime(startTime);
                platformLivetelecastOrderData.setPlatformLivetelecastId(platformLivetelecastOrderDataVo.getBusinessId());
                platformLivetelecastOrderDataMapper.insert(platformLivetelecastOrderData);
            }
        }
        //查询线上活动收入
        List<PlatformLivetelecastOrderDataVo> activityOnlineOrderDataVos = platformLivetelecastOrderDataMapper.selectPlatformLivetelecastOrderDataVo(startTime, endTime, 2);
        for (PlatformLivetelecastOrderDataVo platformLivetelecastOrderDataVo : activityOnlineOrderDataVos) {
            if (platformLivetelecastOrderDataVo != null) {
                PlatformLivetelecastOrderData platformLivetelecastOrderData = new PlatformLivetelecastOrderData();
                platformLivetelecastOrderData.setAmount(platformLivetelecastOrderDataVo.getIncomePrice());
                //活动
                platformLivetelecastOrderData.setType(1);
                //线上
                platformLivetelecastOrderData.setOnlineType(1);
                platformLivetelecastOrderData.setSaveTime(startTime);
                platformLivetelecastOrderData.setPlatformLivetelecastId(platformLivetelecastOrderDataVo.getBusinessId());
                platformLivetelecastOrderDataMapper.insert(platformLivetelecastOrderData);
            }
        }
        //查询线下活动收入
        List<PlatformLivetelecastOrderDataVo> activityOffOrderDataVos = platformLivetelecastOrderDataMapper.selectPlatformLivetelecastOrderDataVo(startTime, endTime, 5);
        for (PlatformLivetelecastOrderDataVo platformLivetelecastOrderDataVo : activityOffOrderDataVos) {
            if (platformLivetelecastOrderDataVo != null) {
                PlatformLivetelecastOrderData platformLivetelecastOrderData = new PlatformLivetelecastOrderData();
                platformLivetelecastOrderData.setAmount(platformLivetelecastOrderDataVo.getIncomePrice());
                //活动
                platformLivetelecastOrderData.setType(1);
                //线下
                platformLivetelecastOrderData.setOnlineType(0);
                platformLivetelecastOrderData.setSaveTime(startTime);
                platformLivetelecastOrderData.setPlatformLivetelecastId(platformLivetelecastOrderDataVo.getBusinessId());
                platformLivetelecastOrderDataMapper.insert(platformLivetelecastOrderData);
            }
        }

    }


}
